layui.use(['treeTable', 'jquery', 'laydate', 'febs', 'form', 'commonJS', 'eleTree', 'validate'], function () {
    var $ = layui.jquery,
        layer = layui.layer,
        febs = layui.febs,
        table = layui.table,
        treeTable = layui.treeTable,
        form = layui.form,
        validate = layui.validate,
        $view = $('#febs-menuNew'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#menuNew-table-form'),
    tableIns;

    form.verify(validate);
    form.render();
    var renderTable = function () {
        tableIns = treeTable.render({
            elem:'#menuNewTable',
            url:ctx + 'menu/treeNew',
            height:'full-200',
            id:'menuNew',
            tree:{
              view:{
                  showIcon:false,
                  dblClickExpand:true
              }
            },
            page:true,
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            cols:[[
                {field: 'name', title: '菜单名称', minWidth: 220},
                {field: 'type',align: "center", title: '菜单类型', width: 120,templet:function (d) {
                        let typeStr = '';
                        if(d.type == '0'){
                            typeStr = '<span class="layui-badge febs-tag-cyan">菜单</span>';
                        }else{
                            typeStr = '<span class="layui-badge febs-tag-orange">按钮</span>';
                        }
                        return typeStr;
                    }},
                {field: 'url', title: '菜单Url', minWidth: 120},
                {field: 'perms', title: '菜单权限', align:'left', width: 220  },
                {field: 'orderNum',align: "center", title: '排序', width: 80},
                {field: 'status',align: "center", title: '菜单状态',toolbar: '#status-bar', width: 120},
                {title: '操作', toolbar: '#menuNew-option',align:'center', width: 240}
            ]],
            toolbar: '#menuNew-toolbar',
            defaultToolbar:'',
            done:function () {
                layer.closeAll('loading');
            },
            error :function (error,data) {
                console.log(error);
            }

        })
    };
    renderTable();

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(menuNewTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增菜单', 'system/menu/add', {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '90%' : '800px','600px'],
                yes: function (index, layero) {
                    $('#menu-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data:{
                }
            });
        }
    });

    table.on('tool(menuNewTable)', function (obj) {
        var data = obj.data.data;
        if (obj.event === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.menuId);
            });
        }
        if (obj.event === 'add') {
            febs.modal.open('新增菜单', 'system/menu/add', {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '90%' : '800px','600px'],
                yes: function (index, layero) {
                    $('#menu-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data:{
                    parentId:data.menuId,
                    parentName:data.menuName
                }
            });
        }
        if (obj.event === 'edit') {
            febs.modal.open('新增菜单', 'system/menu/update/'+data.menuId, {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '90%' : '800px','600px'],
                yes: function (index, layero) {
                    $('#menu-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data:{
                }
            });
        }
    });

    function deleteIds(ids) {
        febs.get(ctx +'menu/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    form.on('switch(status)', function (obj) {
        //区域状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其 postId
        var id =  table.cache['menuNew'][index].id;
        febs.modal.confirm('更改状态', '确认更改状态',function (){
            updateStatus(id,status);
        },function (){
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });
    function updateStatus(id,status) {
        //更新启用状态
        let model={
            menuId : id,
            status : status
        }
        febs.postArray(ctx + 'menu/update', model, function () {
            febs.alert.success('修改状态成功');
        })
    }

    function getQueryParams() {
        return {
            menuName: $searchForm.find('input[name="menuName"]').val()?.trim(),
            invalidate_ie_cache: new Date()
        };
    }
});